

#include "CTrendTable.h"




CTrendTable::CTrendTable(QWidget *widget /* = nullptr*/)
    : QWidget(widget)
{
    InitWidget();
    InitUi();
    InitState();
    InitEvent();
}

void CTrendTable::InitUi()
{
    auto mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);

    auto hbox = new QHBoxLayout;
    hbox->setContentsMargins(0, 0, 0, 0);
    hbox->addWidget(historyView, 10);
    hbox->addWidget(currentView, 1);
    hbox->addWidget(verBar);
    hbox->setSpacing(0);

    mainLayout->addLayout(hbox);
    mainLayout->addWidget(horBar);
}

void CTrendTable::InitEvent()
{
    connect(historyView->horizontalScrollBar(), &QScrollBar::rangeChanged, this, [=](int min, int max) {
        bool visible = historyView->horizontalScrollBar()->maximum() > 0;
        horBar->setVisible(visible);
        horBar->setRange(min, max);
    });

    connect(historyView->horizontalScrollBar(), &QScrollBar::valueChanged, horBar,&QScrollBar::setValue);

    connect(horBar, &QScrollBar::valueChanged, historyView->horizontalScrollBar(), &QScrollBar::setValue);

    connect(historyView->verticalScrollBar(), &QScrollBar::rangeChanged, this, [=](int min, int max) {
        bool visible = historyView->verticalScrollBar()->maximum() > 0;
        verBar->setVisible(visible);
        verBar->setRange(min, max);
    });

    connect(historyView->verticalScrollBar(), &QScrollBar::valueChanged, verBar,&QScrollBar::setValue);

    connect(verBar, &QScrollBar::valueChanged, historyView->verticalScrollBar(), &QScrollBar::setValue);

    connect(verBar, &QScrollBar::valueChanged, currentView->verticalScrollBar(), &QScrollBar::setValue);
}

void CTrendTable::InitWidget()
{
    historyModel = new QStandardItemModel;
//    historyProxyModel = new CFilterProxyModel;
    currentModel = new QStandardItemModel;

    historyView = new QTableView;
    currentView = new QTableView;

    verBar = new QScrollBar(Qt::Vertical);
    horBar = new QScrollBar(Qt::Horizontal);
}

void CTrendTable::InitState()
{
    historyView->setModel(historyModel);
    currentView->setModel(currentModel);

    currentView->verticalHeader()->setVisible(false);

    currentView->setFrameShape(QFrame::Box);
    historyView->setFrameShape(QFrame::Box);

    historyView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    historyView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    currentView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    currentView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    verBar->setVisible(false);
    horBar->setVisible(false);
}

void CTrendTable::AppendData(double time, const QMap<QString, double> &values)
{
    static bool isFirst = true;
    static int last_time;

    if(!isFirst) {
        auto currentItems = currentModel->takeColumn(0);

        QString time_str = QTime(0,0,0).addSecs((int)last_time).toString("hh:mm:ss");
        InsertCols(historyModel,historyModel->columnCount(),time_str,currentItems);
    }
    else
    {
        isFirst = false;
    }

    InsertCols(currentModel,0,"当前值",values);

    last_time = (int)time;
    this->update();
}

void CTrendTable::SetData(const TrendDataList &dataSet)
{
    for (auto data : dataSet)
    {
        QString time_str = QTime(0,0,0).addSecs((int)data->time).toString("hh:mm:ss");
        InsertCols(historyModel,historyModel->columnCount(),time_str,data->dataSet);
    }

    emit PlotFinished();
}

void CTrendTable::SetVerHeader(const QStringList &header)
{
    verHeader = header;

    qobject_cast<QStandardItemModel *>(currentView->verticalHeader()->model())->setVerticalHeaderLabels(header);
    qobject_cast<QStandardItemModel *>(historyView->verticalHeader()->model())->setVerticalHeaderLabels(header);
}

void CTrendTable::Clear()
{
    currentModel->clear();
    historyModel->clear();
}

void CTrendTable::InsertCols(QStandardItemModel * model,int col,const QString & header,const QMap<QString, double> & data)
{
    QList<QStandardItem *> items;
    for(auto name : verHeader) {
        int value = data.value(name);
        items << new QStandardItem(QString::number(value));
    }

    InsertCols(model,col,header,items);
}

void CTrendTable::InsertCols(QStandardItemModel *model, int col, const QString &header, QList<QStandardItem *> items)
{
    model->insertColumn(col,items);
    model->setHorizontalHeaderItem(col, new QStandardItem(header));
}


//void CTrendTable::SetInterval(int val)
//{
//    historyProxyModel->SetInterval(val);
//    historyProxyModel->setFilterKeyColumn(-1);
//}
